﻿namespace Net8.Identity.MvcClient
{
    /// <summary>
    /// 这个服务主要是把授权的记录存储到数据库中，从生产角度上来说，还是有必要的，但开发阶段如果觉得麻烦，可以不要这个服务
    /// </summary>
    public class Worker : IHostedService
    {
        private readonly IServiceProvider _serviceProvider;

        public Worker(IServiceProvider serviceProvider)
            => _serviceProvider = serviceProvider;

        public async Task StartAsync(CancellationToken cancellationToken)
        {
            await using var scope = _serviceProvider.CreateAsyncScope();

            var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
            await context.Database.EnsureCreatedAsync(cancellationToken);
        }

        public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
    }
}
